const express = require('express');

const managerService = require('../service/manager');

const router = express.Router();

// 懒加载查看副卡密列表接口
router.get('/all-sub-card-lazy-load', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取游标和显示数量
    let cursor = parseInt(req.query.cursor) || undefined;
    let limit = parseInt(req.query.limit) || 50;
    if (limit > 50) {
        limit = 50;
    }
    // 调用业务层
    managerService.getAllSubCardLazyLoad(uid, userTypeId, cursor, limit).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 获取所有副卡密接口
router.get('/all-sub-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层
    managerService.getAllSubCard(uid, userTypeId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 删除副卡密接口
router.delete('/sub-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要删除的副卡密ID
    let cardId = req.query.id;
    if (!cardId) {
        return res.status(400).json({
            code: 400,
            msg: "副卡密ID不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.delSubCard(uid, userTypeId, cardId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 修改副卡密接口
router.put('/sub-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要修改的副卡密信息
    let cardId = req.body.cardId;
    let name = req.body.name;
    let url = null;
    if (!name) {
        return res.status(400).json({
            code: 400,
            msg: "卡密名称不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.updateSubCard(uid, userTypeId, cardId, name, url).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 查看副卡密详情接口
router.get('/sub-card', function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要查看的副卡密ID
    let cardId = req.query.id;
    // 调用业务层
    managerService.getSubCardInfo(uid, userTypeId, cardId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 获取当前副卡密下的所有用户卡密信息接口
router.get('/all-user-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层
    managerService.getAllUserCard(uid, userTypeId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 分页获取当前副卡密下的所有用户卡密信息接口
router.get('/all-user-card-by-page', async function (req, res, next) {
    // 获取分页参数
    let page = parseInt(req.query.page) || 1;
    let size = parseInt(req.query.size) || 50;
    if (page < 1) {
        page = 1;
    }
    if (size > 50) {
        size = 50;
    }
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层
    managerService.getAllUserCardByPage(uid, userTypeId, page, size).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 懒加载获取当前副卡密下的所有用户卡密信息接口
router.get('/all-user-card-lazy-load', async function (req, res, next) {
    // 获取懒加载参数
    let cursor = parseInt(req.query.cursor) || undefined;
    let limit = parseInt(req.query.limit) || 50;
    if (limit > 50) {
        limit = 50;
    }
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层
    managerService.getAllUserCardLazyLoad(uid, userTypeId, cursor, limit).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 搜索副卡密接口
router.get('/search-sub-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取搜索的副卡密
    let card = req.query.card;
    if (!card) {
        return res.status(400).json({
            code: 400,
            msg: "搜索卡密不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.searchSubCard(uid, userTypeId, card).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 搜索用户卡密接口
router.get('/search-user-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取搜索的用户卡密
    let card = req.query.card;
    if (!card) {
        return res.status(400).json({
            code: 400,
            msg: "搜索卡密不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.searchUserCard(uid, userTypeId, card).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 查看用户卡密详情接口
// 主要是查看跳转的url和name
router.get('/user-card', function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要查看的用户卡密ID
    let id = req.query.id;
    if (!id) {
        return res.status(400).json({
            code: 400,
            msg: "卡密ID不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.getUserCardInfo(uid, userTypeId, id).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 懒加载查看某个副卡密下的用户卡密列表接口
router.get('/all-user-card-by-sub-card-lazy-load', async function (req, res, next) {
    // 获取懒加载参数
    let cursor = parseInt(req.query.cursor) || undefined;
    let limit = parseInt(req.query.limit) || 50;
    if (limit > 50) {
        limit = 50;
    }
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要查看的副卡密ID
    let cardId = req.query.id;
    if (!cardId) {
        return res.status(400).json({
            code: 400,
            msg: "副卡密ID不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.getAllUserCardBySubCardLazyLoad(uid, userTypeId, cardId, cursor, limit).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 分片查看某个副卡密下的用户卡密列表接口

// -------------------------- 当前进度 --------------------------

// 懒加载获取待处理的用户卡密接口
router.get('/unenable-user-card-lazy-load', async function (req, res, next) {
    // 获取懒加载参数
    let cursor = parseInt(req.query.cursor) || undefined;
    let limit = parseInt(req.query.limit) || 50;
    if (limit > 50) {
        limit = 50;
    }
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层
    managerService.getUnenableUserCardLazyLoad(uid, userTypeId, cursor, limit).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 授权用户卡密接口 - url + isEnable
router.put('/enable-user-card', function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取卡密id
    let userCardId = req.body.userCardId;
    // 调用业务层
    managerService.enableUserCard(uid, userTypeId, userCardId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 删除用户卡密接口 - 副卡密才有的权限
router.delete('/user-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取要删除的副卡密ID
    let cardId = req.query.id;
    if (!cardId) {
        return res.status(400).json({
            code: 400,
            msg: "用户卡密ID不允许为空",
            data: null
        });
    }
    // 调用业务层
    managerService.delUserCard(uid, userTypeId, cardId).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

module.exports = router;
